/**
 * Dear programmer:
 * When I wrote this code, only god and I knew how it worked.
 * Now, only god knows it!
 *
 * @author: Pegasus  Date: 2018/11/21 Time: 10:03
 *
 * 工作
 */

import React, {Component} from 'react';
import {
    SafeAreaView,
    View,
    Text,
    Image,
    ScrollView,
    TouchableOpacity,
    TouchableHighlight, Platform, BackHandler, ToastAndroid, FlatList, Modal
} from 'react-native';
import {connect} from 'react-redux';
import {styles} from './RoomsViewScreen.Style';
import OffWorkView from "../../../component/OffWorkView/OffWorkView"
import {RetrieveRoomsViewAs,HoReservationDefault} from "./Actions";
import {orderSweepSave} from "../../Housekeeper/orderScreen/Actions";
import {FetchStatus} from "../../../configuration/RequestConfiguration";
import {ReturnStatus} from "../../../configuration/ReturnStatus";
import {doDisplayWarnMessage} from "../../../component/Message/Action";
import AntDesign from "react-native-vector-icons/AntDesign";
import Feather from "react-native-vector-icons/Feather";
import FontAwesome from "react-native-vector-icons/FontAwesome";
import LoadingView from "../../../component/LoadingView/LoadingView";
import User from "../../../model/User/User";
import {SCREEN_WIDTH} from "../../../constant/PHScreen";
import {COLOR_GRAY_LINE, FONT_COLOR_TITLE} from "../../../constant/PHColors"
import global from "../../../configuration/global";
import {ContextPath} from "../../../configuration/AppConfig";

class RoomsViewScreen extends Component {

    static navigationOptions = ({navigation}) => ({
        headerTitle: ``,
        headerLeft: <View><Text style={{marginLeft:15,fontSize:18,color:FONT_COLOR_TITLE}}>房态盘</Text></View>,
        // headerRight: <OffWorkView navigation={navigation}/>
    });

    // 构造
    constructor(props) {
        super(props);
        // 初始状态
        this.state = {
            RoomsViewArr:[],
            roomSearchStr:{"wherestatus":{"All":"1"},"arg":{"arg":"房务"},"where":{"building":"","rmtype":"","floor":"","allSearch":"","source":"","market":"","features":"","grno":""}},//搜索条件
            ftpnoicoArr:[],  //记录房态盘不显示图标合集
            INHouse:0, //在住数量
        };
    }

    componentWillReceiveProps(nextProps, nextContext): void {
        let {dispatch} = this.props;
        if (nextProps.onRetrieveRoomsView !== this.props.onRetrieveRoomsView && nextProps.onRetrieveRoomsView.fetchStatus === FetchStatus.Success) {
            if (nextProps.onRetrieveRoomsView.RETURN === ReturnStatus.Success) {
                let RoomsViewArr = nextProps.onRetrieveRoomsView.DATA;
                let INHouse = 0;
                console.log('数据接收成功');//提示
                RoomsViewArr.map((item,index)=>{
                    if(item.INHouse==="1"){
                        INHouse = INHouse+1;
                    }
                });

                this.setState({
                    RoomsViewArr: RoomsViewArr,
                    INHouse: INHouse,
                });
            } else {
                dispatch(doDisplayWarnMessage(nextProps.onRetrieveRoomsView.MSG))
            }
        }

        if (nextProps.onReservationDefault !== this.props.onReservationDefault && nextProps.onReservationDefault.fetchStatus === FetchStatus.Success) {
            if (nextProps.onReservationDefault.RETURN === ReturnStatus.Success) {
                let ftpnoico = nextProps.onReservationDefault.ftpnoico.toLocaleLowerCase().split(",");
                let data = new FormData();
                data.append("hotelId", global.hotelId);

                ToastAndroid.show('正在加载房态盘', 1000);//提示
                try {
                    fetch(ContextPath()+ '/frontofficeTimer/getRoomsViewDataCash', {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'multipart/form-data;charset=utf-8'
                        },
                        body: data
                    }).then(function(response) {
                        return response.json();
                    }).then(data=>{
                        console.log("*****************************"+data.length);

                        let RoomsViewArr = data;
                        let INHouse = 0;
                        RoomsViewArr.map((item,index)=>{
                            if(item.INHouse==="1"){
                                INHouse = INHouse+1;
                            }
                        })

                        this.setState({
                            RoomsViewArr: RoomsViewArr,
                            INHouse: INHouse,
                            ftpnoicoArr: ftpnoico,
                        });
                    })
                } catch (err) {
                    let str = this.state.roomSearchStr;
                    dispatch(RetrieveRoomsViewAs(JSON.stringify(str)));
                }
            } else {
                dispatch(doDisplayWarnMessage(nextProps.onReservationDefault.MSG))
            }
        }

    }

    // 组件渲染后调用
    componentDidMount() {
        let {dispatch} = this.props;
        let param = {"arg":{"arg":"初始化","filed":""},"ho_guest":{"arr_d":"","nights":"","exp_dep_d":"","adults":"","form-field-radio":"on","market":"","source":"","pno":"","comp_id":"","m_cardno":"","rcode":""},"ho_profile":{"pno":""}};
        dispatch(HoReservationDefault(JSON.stringify(param)));
    }

    render() {
        let {dispatch,onRetrieveRoomsView} = this.props;

        if (this.state.RoomsViewArr_og.length===0) {
            return <LoadingView/>
        }else{
            return (
                <SafeAreaView style={styles.container}>
                    {/*确认模态*/}
                    <Modal visible={this.state.moreOperateModal} animationType = {'none'} transparent = {true} onRequestClose={()=> this.onRequestClose()} >
                        <TouchableOpacity style={{flex: 1}} activeOpacity={1}  onPress={()=>{
                            this.onRequestClose();
                        }}>
                            <View style={styles.checkmodalContainer} >
                                <View style={styles.checkModalBody}>
                                    <TouchableOpacity activeOpacity={1} onPress={()=>{
                                        this.state.showCleanRoom = !this.state.showCleanRoom;
                                        this.changeRoomsViewArr()
                                    }}>
                                        <View style={styles.checkModalLine}>
                                            {
                                                (this.state.showCleanRoom)
                                                    ?
                                                    (
                                                        <Image style={{width: 18, height: 18}} source={require('../../../resource/login/dl_yixuan.png')}/>
                                                    )
                                                    :
                                                    (
                                                        <Image style={{width: 18, height: 18}} source={require('../../../resource/login/dl_weixuan.png')}/>
                                                    )
                                            }
                                            <Text style={{fontSize: 16}}>
                                                显示干净房
                                            </Text>
                                        </View>
                                    </TouchableOpacity>
                                </View>
                            </View>
                        </TouchableOpacity>
                    </Modal>
                </SafeAreaView>
            )
        }

    }

}

const props = state => {
    return {
        onRetrieveRoomsView: state.onRetrieveRoomsView,
        onReservationDefault: state.onReservationDefault,
        onOrderSweepSave:state.onOrderSweepSave,
    }
};

export default RoomsViewScreen = connect(props)(RoomsViewScreen);
